import cluster from 'node:cluster';

import http from 'node:http';

import { cpus } from 'node:os';

const numCPUs = cpus().length;
// 根据 cpu 个数来跑了 n 个进程，每个进程跑了一个服务器，这样请求会分配到多个服务器中去。
if (cluster.isPrimary) {
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
    cluster.on('fork', (worker) => {
        console.log('worker 创建成功', worker.id);
    });

    cluster.on('exit', (worker, code, signal) => {
        console.log('worker 退出:', worker.id);
    });

} else {
    const server = http.createServer((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
    })

    server.listen(8000);

    setTimeout(() => {
        process.exit();
    }, 3000)
}